package com.tyut.controller;

import com.tyut.pojo.User;
import com.tyut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login1",method = RequestMethod.GET)
    public String toLogin() {
        return "login";
    }

//    @RequestMapping(value = "/login" ,method = RequestMethod.POST)
//    public String login(User user, HttpSession session, Model model) {
//
//        String username = user.getUsername();
//        String password = user.getPassword();
//
//        User queryUser = userService.selectByUsername(user);
//        if (queryUser != null) {
//            session.setAttribute("USER_SESSION", user);
//            return "index";
//        }
//        model.addAttribute("msg", "用户名或密码错误，请重新登录！");
//        return "login";
//    }
@RequestMapping("/check")
@ResponseBody
    public String check(@RequestBody User user,HttpSession session){
        String username = user.getUsername();
        String password = user.getPassword();
        User user1 = this.userService.findUserByName(username);
        if (user1!=null){
            if(user1.getPassword().equals(password)){
                session.setAttribute("USER_SESSION",user);
                return "1";
            }
        }
        return "0";
    }

    @RequestMapping("/index")
    public String toMain() {
        return "index";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:login1";
    }

    @RequestMapping("/register")
    public String registerUser(String username,String psd1,String psd2,Model model) {
        User user1 = new User();
        if(username==""||psd1==""||psd2=="") {
            model.addAttribute("error","未按正确格式注册！请重新注册！");
            return "register";
        }else if(userService.selectByUsername(username)!=null){
            model.addAttribute("error","该用户名已经存在，请重新注册！");
            return "register";
        }else if(psd1.equals(psd2)&&psd1!="") {
            user1.setUsername(username);
            user1.setPassword(psd1);
            userService.insertUser(user1);
            model.addAttribute("msg","注册成功！");
            return "register";
        }else {
            model.addAttribute("error","两次填写密码不一致！请重新填写！");
            return "register";
        }
    }

    @RequestMapping("/revokeUser")
    public String revokeUser(User user,HttpSession session) {
        String username = user.getUsername();
        userService.deleteUser(username);
        session.invalidate();
        return "login";
    }

    @RequestMapping("/alterUser")
    public String alterUser(HttpServletRequest request, Model model) {
        String username = request.getParameter("username");
        String oldPassword = request.getParameter("oldpsd");
        String newPassword = request.getParameter("newpsd");
        String password = userService.selectByUsername(username).getPassword();
        User user = new User();
        user.setUsername(username);
        user.setPassword(newPassword);
        if(password.equals(oldPassword)) {
            userService.updateUser(user);
            return "login";
        }else if(oldPassword==""||newPassword=="") {
            model.addAttribute("msg","密码输入为空！请重新输入！");
            return "useralter";
        }else {
            model.addAttribute("msg","原密码输入错误！");
            return "useralter";
        }

    }

    @RequestMapping("/userAlterJSP")
    public String userAlterJSP(){
        return "useralter";
    }
    @RequestMapping("/registerJSP")
    public String registerJSP(){
        return "register";
    }

}
